from tornado_request_mapping import request_mapping

from common.base_handler import BasicHandler
from common.decorator import login_required
from schemas.user_schema import UserSchema, UserDeleteSchema, UserLoginSchema
from services.user_service import UserService


@request_mapping("/user")
class UserManageHandler(BasicHandler):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.service = UserService()

    @request_mapping("/create", 'post', '创建用户')
    @login_required('admin')
    async def create_user(self):
        data = self.data
        params = self.check_params(UserSchema, **data)
        response = await self.service.create_user(params)
        return self.send_response(response.code, response.data, response.msg)

    @request_mapping("/delete", 'delete', '删除用户')
    @login_required('admin')
    async def delete_user(self):
        data = self.data
        params = self.check_params(UserDeleteSchema, **data)
        response = await self.service.delete_user(params)
        return self.send_response(response.code, response.data, response.msg)

    @request_mapping("/list", 'get', '用户列表')
    # @login_required()
    async def list_user(self):
        response = await self.service.list_users(self.data)
        return self.send_response(response.code, response.data, response.msg)

    @request_mapping("/login", 'post', '登录')
    async def login(self):
        data = self.data
        params = self.check_params(UserLoginSchema, **data)
        response = await self.service.login(params)
        return self.send_response(response.code, response.data, response.msg)
